<?php
namespace app\store\controller\manager;

use app\store\controller\Base;
use app\store\controller\Weixin;
use think\Db;
use think\Session;

class Index extends Base
{
	/**
	 * 店长首页
	 * URL : store/manager.index/index
	 * @return [type] [description]
	 */

    public function index()
    {

        //广告图
        $banner_info =  Db('banner')
            ->where('shop_id',$this->shop_id)
            ->where('store_id',['=',1],['=',0],'or')
            ->where('is_show',1)
            ->where('is_del',0)
            ->field('pic_url,url')
            ->select();
        $this->assign('banner_info',$banner_info);

        //公告
        $notice_info = Db('notice')
            ->where('shop_id',$this->shop_id)
            ->where('store_id',['=',1],['=',0],'or')
            ->where('is_show',1)
            ->field('title')
            ->select();
        $this->assign('notice_info',$notice_info);


        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch();
    }

    /**
     * 个人中心
     * wangtiantian
     */
    public function mycenter(){
        $res = Db::name('teacher')->where('teacher_id',$this->uid)->find();
        $this->assign('res',$res);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch('myCenter');
    }

    /**
     * wangtiantian
     * 编辑个人信息
     * @return mixed
     */
    public function edit(){

        if(request()->isAjax()){
            $d = input();
            $data['headimgurl'] = $d['headimgurl'];
            $data['teacher_tel'] = $d['tel'];
            $data['teacher_name'] = $d['name'];
            $data['sex'] = $d['sex'];



            if($data['headimgurl'] == null || $data['headimgurl'] == "undefined"){

                $data['headimgurl'] = Db::name('teacher')->where('teacher_id',$this->uid)->value('headimgurl');
            }

            $res = Db::name('teacher')->where('teacher_id',$this->uid)->update($data);

            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }


        }

        $res= Db::name('teacher')->where('teacher_id',$this->uid)->find();
        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('res', $res);
        $this->assign('shop_id',$this->shop_id);
        return  $this->fetch();
    }

    /*
     * 微信授权
     * 获取微信授权信息 openid
     * wangtiantian
     */
    public function admin_wxinfo(){

        if(input()){
            $wx_info = json_decode(input('back_info'),true);

            $data['openid'] =  $wx_info['openid'];
            $data['nickname'] =  $wx_info['nickname'];
            $data['headimgurl'] =  $wx_info['headimgurl'];
            $data['shop_id'] =  input('shop_id');
            $data['store_id'] =  input('store_id');
            //           $data['type'] =  input('type');/
            $data['role'] =  2;
            $data['join_type'] =  0;

            $test = Db::name('teacher')->where('openid',$data['openid'])->find();

            if(!$test){
                $res = Db::name('teacher')->insertGetId($data);
                if($res){
                    Session::set('shop_id',$data['shop_id'] );
                    Session::set('uid',$res );
                    $this->success('绑定成功','index');
                }

            }else{
//                Session::set('shop_id',$test['shop_id'] );
//                Session::set('uid',$test['teacher_id'] );
                $this->error('该微信已被绑定，请勿重复操作');
            }



        }



    }

}
